﻿using PlayerRoles;
using PlayerStatsSystem;
using PluginAPI.Core;
using System;
using System.Collections.Generic;
using Utils.ModuleAHP;
using Utils.ModuleHealth;


namespace Utils
{
    /// <summary>
    /// 助理|玩家类
    /// </summary>
    public class PlayerUtils
    {
        /// <summary>
        /// 获取 阵营角色
        /// </summary>
        public static bool TryGetPlayers(Team team, int maxCount, out Player[] output)
        {
            output = null;

            List<Player> players = Player.GetPlayers().FindAll(e => e.Team == team);
            if (players.Count == 0) return false;

            players.RemoveAll(e => e.Role == RoleTypeId.Tutorial || e.Role == RoleTypeId.Overwatch || e.Role == RoleTypeId.CustomRole);

            if (maxCount == -1)
            {
                output = players.ToArray();
                return true;
            }


            if (players.Count <= maxCount)
            {
                output = players.ToArray();
                return true;
            }

            output = players.GetRange(0, maxCount).ToArray();
            return true;
        }

        /// <summary>
        /// 获取 阵营角色
        /// </summary>
        public static bool TryGetPlayers(RoleTypeId roleTypeId, int maxCount, out Player[] output)
        { 
            output = null;

            var match = Player.GetPlayers().FindAll(e => e.Role == roleTypeId);
            if (match.Count == 0) return false;

            if (maxCount == -1)
            {
                output = match.ToArray();
                return true;
            }

            if (match.Count <= maxCount)
            { 
                output = match.ToArray();
                return true;
            }

            output = match.GetRange(0, maxCount).ToArray();
            return true;
        }

        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        /// <param name="health"></param>
        /// <param name="maxHealth"></param>
        public static void SetHP(Player player, float health, float maxHealth)
        {
            CustomHealthStat healthStat = new CustomHealthStat() { Hub = player.ReferenceHub };

            player.ReferenceHub.playerStats._dictionarizedTypes[typeof(HealthStat)]
                = player.ReferenceHub.playerStats.StatModules[Array.IndexOf(PlayerStats.DefinedModules, typeof(HealthStat))]
                = healthStat;

            healthStat.Init(player.ReferenceHub);
            healthStat._MaxValue = maxHealth;

            player.Health = health; 
        }


        public static void SetDefaultHP(Player player)
        {
            HealthStat healthStat = new HealthStat() { Hub = player.ReferenceHub };

            player.ReferenceHub.playerStats._dictionarizedTypes[typeof(HealthStat)]
                = player.ReferenceHub.playerStats.StatModules[Array.IndexOf(PlayerStats.DefinedModules, typeof(HealthStat))]
                = healthStat;

            healthStat.Init(player.ReferenceHub);
            player.Health = 100;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        /// <param name="artificalHealth"></param>
        /// <param name="maxArtificalHealth"></param>
        public static void SetAP(Player player, float artificalHealth, float maxArtificalHealth)
        {
            CustomAHPStat aHpStat = new CustomAHPStat() { Hub = player.ReferenceHub };

            player.ReferenceHub.playerStats._dictionarizedTypes[typeof(AhpStat)]
                = player.ReferenceHub.playerStats.StatModules[Array.IndexOf(PlayerStats.DefinedModules, typeof(AhpStat))]
                = aHpStat;

            aHpStat.Init(player.ReferenceHub);
            aHpStat._MaxValue = maxArtificalHealth;

            player.ArtificialHealth = artificalHealth;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        /// <param name="healAmount"></param>
        /// <param name="isOverride"></param>
        public static void Heal(Player player, float healAmount, bool isOverride)
        {
            if (isOverride)
            {
                player.Health += healAmount;
                return;
            }

            player.Heal(healAmount);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        public static void AddAmmo(Player player)
        {
            player.AddItem(ItemType.Ammo12gauge);
            player.AddItem(ItemType.Ammo12gauge);
            player.AddItem(ItemType.Ammo12gauge);
            player.AddItem(ItemType.Ammo12gauge);
            player.AddItem(ItemType.Ammo12gauge);


            player.AddItem(ItemType.Ammo44cal);
            player.AddItem(ItemType.Ammo44cal);
            player.AddItem(ItemType.Ammo44cal);
            player.AddItem(ItemType.Ammo44cal);
            player.AddItem(ItemType.Ammo44cal);


            player.AddItem(ItemType.Ammo556x45);
            player.AddItem(ItemType.Ammo556x45);
            player.AddItem(ItemType.Ammo556x45);
            player.AddItem(ItemType.Ammo556x45);
            player.AddItem(ItemType.Ammo556x45);
            player.AddItem(ItemType.Ammo556x45);

            player.AddItem(ItemType.Ammo762x39);
            player.AddItem(ItemType.Ammo762x39);
            player.AddItem(ItemType.Ammo762x39);
            player.AddItem(ItemType.Ammo762x39);
            player.AddItem(ItemType.Ammo762x39);


            player.AddItem(ItemType.Ammo9x19);
            player.AddItem(ItemType.Ammo9x19);
            player.AddItem(ItemType.Ammo9x19);
            player.AddItem(ItemType.Ammo9x19);
            player.AddItem(ItemType.Ammo9x19);
        }
    }
}
